/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D;

import czStvDpds.graphics3D.geometry.Object3D;
import java.util.Comparator;
import org.lwjgl.util.vector.Matrix4f;

/**
 *
 * @author divad
 */
public class GraphicsObjectComparator implements Comparator {

    private int sortFunction;
    private Matrix4f camMatrix;

    public GraphicsObjectComparator() {
        this.camMatrix = new Matrix4f();
    }

    public int getSortFunction() {
        return sortFunction;
    }

    public void setSortFunction(int sortType) {
        this.sortFunction = sortType;
    }

    public void setCamMatrix(Matrix4f camMatrix) {
        this.camMatrix = camMatrix;
    }

    public Matrix4f getCamMatrix() {
        return camMatrix;
    }

    @Override
    public int compare(Object o1, Object o2) {
        if (o1 instanceof Object3D && o2 instanceof Object3D) {
            Object3D obj1 = (Object3D) o1;
            Object3D obj2 = (Object3D) o2;
            float z1 = this.camMatrix.m02 * obj1.getPosMatrix().m30
                    + this.camMatrix.m12 * obj1.getPosMatrix().m31
                    + this.camMatrix.m22 * obj1.getPosMatrix().m32
                    + this.camMatrix.m32 * obj1.getPosMatrix().m33;
            float z2 = this.camMatrix.m02 * obj2.getPosMatrix().m30
                    + this.camMatrix.m12 * obj2.getPosMatrix().m31
                    + this.camMatrix.m22 * obj2.getPosMatrix().m32
                    + this.camMatrix.m32 * obj2.getPosMatrix().m33;

            if (this.sortFunction == SceneSettings.SORT_BY_Z_FROM_GREATER) {
                if (z1 < z2) {
                    return -1;
                } else {
                    return 1;
                }
            } else if (this.sortFunction == SceneSettings.SORT_BY_Z_FROM_LESS) {
                if (z1 < z2) {
                    return 1;
                } else {
                    return -1;
                }
            }
        }
        return 0;
    }
}
